<template>
  <header class="navbar" :class="{ 'showNavbarColor': isLiner }">
    <SidebarButton @toggle-sidebar="$emit('toggle-sidebar')" />
    <router-link :to="$localePath" class="home-link">
      <img class="logo" v-if="$themeConfig.logo" :src="$withBase($themeConfig.logo)" :alt="$siteTitle">
      <span ref="siteName" class="site-name" v-if="$siteTitle">{{ $siteTitle }}</span>
    </router-link>

    <div class="links" :style="linksWrapMaxWidth ? {
      'max-width': linksWrapMaxWidth + 'px'
    } : {}">
      <!-- 写一个开关 -->

      <Mode />
      <AlgoliaSearchBox v-if="isAlgoliaSearch" :options="algolia" />
      <SearchBox v-else-if="$themeConfig.search !== false && $frontmatter.search !== false" />
      <NavLinks class="can-hide" />
      <div class="switch_wapper">
        <input type="checkbox" name="" id="on" v-model="switchFlag" @change="switchChange($event)">

        <label class="switch" for="on">
          <span class="ball"></span>
        </label>
      </div>
    </div>
</header>
</template>

<script>


import AlgoliaSearchBox from '@AlgoliaSearchBox'
import SearchBox from '@SearchBox'
import SidebarButton from '@theme/components/SidebarButton'
import NavLinks from '@theme/components/NavLinks'
import Mode from '@theme/components/Mode'

export default {
  components: { SidebarButton, NavLinks, SearchBox, AlgoliaSearchBox, Mode },

  data() {
    return {
      linksWrapMaxWidth: null,
      switchFlag: true,
      scrollTop: 0,
      isLiner: false,
      clientHeight: 100,
    }
  },
  props: {
    isHome: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    scrollTop(nv) {
      const fHeight = this.isHome ? this.clientHeight : this.clientHeight / 2;
      if (nv >= fHeight) {
        this.isLiner = true;
      } else {
        this.isLiner = false;
      }
    },
  },
  mounted() {
    const MOBILE_DESKTOP_BREAKPOINT = 719 // refer to config.styl
    const NAVBAR_VERTICAL_PADDING = parseInt(css(this.$el, 'paddingLeft')) + parseInt(css(this.$el, 'paddingRight'))
    const handleLinksWrapWidth = () => {
      if (document.documentElement.clientWidth < MOBILE_DESKTOP_BREAKPOINT) {
        this.linksWrapMaxWidth = null
      } else {
        this.linksWrapMaxWidth = this.$el.offsetWidth - NAVBAR_VERTICAL_PADDING -
          (this.$refs.siteName && this.$refs.siteName.offsetWidth || 0)
      }
    }
    handleLinksWrapWidth()
    window.addEventListener('resize', handleLinksWrapWidth, false)
    window.addEventListener('load', this.switchChange, false)
    window.addEventListener("scroll", this.handleScroll);
  },

  computed: {
    algolia() {
      return this.$themeLocaleConfig.algolia || this.$themeConfig.algolia || {}
    },

    isAlgoliaSearch() {
      return this.algolia && this.algolia.apiKey && this.algolia.indexName
    }
  },
  methods: {
    switchChange(e) {
      // 获取当前的值
      this.$nextTick(() => {
        if (this.switchFlag) {
          bgCanvas && (bgCanvas.style.display = 'inherit');
        } else {
          bgCanvas && (bgCanvas.style.display = 'none');
        }
      })
    },
    handleScroll() {
      this.scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
      this.clientHeight = document.documentElement.clientHeight;
    },
  }
}

function css(el, property) {
  // NOTE: Known bug, will return 'auto' if style value is 'auto'
  const win = el.ownerDocument.defaultView
  // null means not to return pseudo styles
  return win.getComputedStyle(el, null)[property]
}
</script>
<style lang="stylus">
@require '../styles/mode.styl'

$navbar-vertical-padding = 0.7rem
$navbar-horizontal-padding = 1.5rem

.navbar
  padding $navbar-vertical-padding $navbar-horizontal-padding
  line-height $navbarHeight - 1.4rem
  box-shadow var(--box-shadow)
  background var(--background-color)
  a, span, img
    display inline-block
  .logo
    height $navbarHeight - 1.4rem
    min-width $navbarHeight - 1.4rem
    margin-right 0.8rem
    vertical-align top
    border-radius 50%
  .site-name
    font-size 1.2rem
    font-weight 600
    color var(--text-color)
    position relative
    background var(--background-color)
  .links
    padding-left 1.5rem
    box-sizing border-box
    white-space nowrap
    font-size 0.9rem
    position absolute
    right $navbar-horizontal-padding
    top $navbar-vertical-padding
    display flex
    background-color var(--background-color)
    .search-box
      flex: 0 0 auto
      vertical-align top

@media (max-width: $MQMobile)
  .navbar
    padding-left 4rem
    .can-hide
      display none
    .links
      padding-left .2rem
</style>
<style lang="stylus" scoped>
.switch_wapper{
  height:1.5rem;
  width:60px;
  align-self: center;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}
[type='checkbox'] {
  display: none;
}

.switch {
  width: 50px;
  height: 18px;
  background: transparent;
  display: inline-block;
  border-radius: 18px;
  border: 1px solid #FFF;
  border-right: 0;
}

.switch>.ball {
  display: block;
  width: 13px;
  height: 13px;
  border-radius: 13px;
  background: transparent;
  border: 2px solid #FFF;
  margin-left: 31px;
}

[type='checkbox']:checked+label {
  background: $accentColor;
  border:0;
}

[type='checkbox']:checked+label>.ball {
  margin: 0px;
}
</style>